package org.streets.eis.component.grid;


import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;
import org.streets.eis.component.ChangedNotifyType;
import org.streets.eis.component.model.ListItem;
import org.streets.eis.component.organcombobox.OrganComboBox;

import com.inmethod.grid.column.AbstractColumn;

public class OrganComboBoxColumn extends EditableColumn {
	private static final long serialVersionUID = 1L;
	
	private ChangedNotifyType changedNotifyType = ChangedNotifyType.NONE;
	
	private boolean canInput;
	
	private String[] rootOrganIds;
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty) {
		this(columnId, headerModel, propertyExpression, sortProperty,false);
	}

	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,boolean canInput) {
		this(columnId, headerModel, propertyExpression, sortProperty,canInput,new String[]{});
	}
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,boolean canInput,String... rootOrganId) {
		super(columnId, headerModel, propertyExpression, sortProperty);
		this.canInput = canInput;
		this.rootOrganIds = rootOrganId;
	}
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(String columnId, IModel headerModel, String propertyExpression) {
		this(columnId, headerModel, propertyExpression,false);
	}
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(String columnId, IModel headerModel, String propertyExpression,boolean canInput) {
		this(columnId, headerModel, propertyExpression,canInput,new String[]{});
	}
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(String columnId, IModel headerModel, String propertyExpression,boolean canInput,String... rootOrganId) {
		super(columnId, headerModel, propertyExpression);
		this.canInput = canInput;
		this.rootOrganIds = rootOrganId;
	}

	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(IModel headerModel, String propertyExpression, String sortProperty) {
		this(headerModel, propertyExpression, sortProperty,false);
	}

	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(IModel headerModel, String propertyExpression, String sortProperty,boolean canInput) {
		this(headerModel, propertyExpression, sortProperty,canInput,new String[]{});
	}
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(IModel headerModel, String propertyExpression, String sortProperty,boolean canInput,String... rootOrganId) {
		super(headerModel, propertyExpression, sortProperty);
		this.canInput = canInput;
		this.rootOrganIds = rootOrganId;
	}
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(IModel headerModel, String propertyExpression) {
		this(headerModel, propertyExpression,false);
	}	
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(IModel headerModel, String propertyExpression,boolean canInput) {
		this(headerModel, propertyExpression,canInput,new String[]{});
	}	
	
	@SuppressWarnings("unchecked")	
	public OrganComboBoxColumn(IModel headerModel, String propertyExpression,boolean canInput,String... rootOrganId) {
		super(headerModel, propertyExpression);
		this.canInput = canInput;
		this.rootOrganIds = rootOrganId;
	}	
	
	@SuppressWarnings("unchecked")
	@Override
	protected FormComponent newEditableComponent(String id, final IModel model, IModel rowModel, AbstractColumn column){
		beforeNewComboBox(model,rowModel);
		return new OrganComboBox(id, model,canInput,rootOrganIds){
			private static final long serialVersionUID = 1L;
			@Override
			protected ChangedNotifyType getChangedNotifyType(){
				return OrganComboBoxColumn.this.getChangedNotifyType();
			}
			@Override
			protected void onNoPopup(AjaxRequestTarget target){
				OrganComboBoxColumn.this.onNoPopup(this,target);
			}			
			@Override
			protected void onItemChanged(OrganComboBox organComboBox,ListItem newValue){
				OrganComboBoxColumn.this.onItemChanged(organComboBox,newValue);
			}			
			@Override
			protected void onAjaxItemChanged(OrganComboBox organComboBox,ListItem newValue,AjaxRequestTarget target){
				OrganComboBoxColumn.this.onAjaxItemChanged(organComboBox,newValue,target);
			}
		};
	}
	
	public ChangedNotifyType getChangedNotifyType() {
		return changedNotifyType;
	}

	public void setChangedNotifyType(ChangedNotifyType changedNotifyType) {
		this.changedNotifyType = changedNotifyType;
	}	
	
	public boolean isCanInput() {
		return canInput;
	}

	public void setCanInput(boolean canInput) {
		this.canInput = canInput;
	}
	
	public String[] getRootOrganIds() {
		return rootOrganIds;
	}

	public void setRootOrganIds(String... rootOrganId) {
		this.rootOrganIds = rootOrganId;
	}
	
	@SuppressWarnings("unchecked")
	protected void beforeNewComboBox(final IModel model, IModel rowModel){
	}
	
	protected void onNoPopup(FormComponent<?> component,AjaxRequestTarget target){
	}
	
	protected void onItemChanged(FormComponent<?> component,ListItem newValue){		
	}

	protected void onAjaxItemChanged(FormComponent<?> component,ListItem newValue,AjaxRequestTarget target){
	}
	
}
